package com.fram.controller.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.recovery.request.RecoveryFormRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.entity.RecoveryForm;
import com.fram.service.api.v1.RecoveryForService;

@RestController
@RequestMapping("/api/{version}/recovery")
public class RecoveryForController extends BaseController{
	
	private static Logger LOG = LoggerFactory.getLogger(RecoveryForController.class);
	
	@Autowired
	private RecoveryForService recoveryForService;
	
	/** 先查看某个用户的 收货订单 哪些可以 回收了。**/
	
	/**添加到回收表 （然后提醒 店家）(未提示功能 )**/
	@ApiVersion(1.0)
	@RequestMapping("add")
	private ResponseMsg add(@RequestBody List<RecoveryFormRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			recoveryForService.adds(request);
		} catch (ProException e) {
			LOG.error("-添加 要回收的商品->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-添加 要回收的商品->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/**查询可回收的**/
	@ApiVersion(1.0)
	@RequestMapping("recyclable")
	private ResponseMsg Recyclable() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(recoveryForService.Recyclable());
		} catch (ProException e) {
			LOG.error("-可回收列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-可回收列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/**查询可回收的  第二版展示**/
	@ApiVersion(2.0)
	@RequestMapping("recyclable")
	private ResponseMsg Recyclables() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(recoveryForService.Recyclables());
		} catch (ProException e) {
			LOG.error("-可回收列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-可回收列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	/**展示申請列表**/
	@ApiVersion(1.0)
	@RequestMapping("list")
	private ResponseMsg list(@RequestBody PageBean<RecoveryForm> request) {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			if(getApiUserId() != null) {
				request.getParam().setUserId(getApiUserId());
			}
			msg.setData(recoveryForService.list(request));
		} catch (ProException e) {
			LOG.error("-回收列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-回收列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
}
